package com.leadnews.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leadnews.mapper.LevelMapper;
import com.leadnews.pojo.Level;
import com.leadnews.pojo.User;
import com.leadnews.service.UserService;
import com.leadnews.mapper.UserMapper;
import com.leadnews.utils.AppJwtUtil;
import com.leadnews.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author 自己
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2023-01-01 07:24:04
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService{
    @Autowired
    UserMapper userMapper;
    @Override
    public ResponseResult login(User user) {
        ResponseResult result = new ResponseResult();
        UserVo userVo = new UserVo();
        try {
            User user1 = userMapper.findByMailOrPas(user.getUsername(),user.getPassword());
            if(user1==null){
                result.setCode(0);
                result.setErrorMessage("用户不存在，请重新输入");
                return result;
            }
            userVo.setId(user1.getId());
            Level level=levelMapper.finduserid(user1.getId()+"");
            userVo.setPicture(level.getPicture());
            userVo.setName(user1.getUsername());
            String token = AppJwtUtil.getToken(Long.valueOf(user1.getId()));
            result.setHost(token);
            result.setData(userVo);
            result.setCode(200);
        }catch (Exception e){
            result.setCode(0);

        }
        return result;
    }

    @Override
    public PageResultVo getUserList(PageVo pageVo) {
        PageResultVo resultVo = new PageResultVo();
        System.out.println(pageVo);
        User user = pageVo.getUser();

        String currentPage = pageVo.getCurrentPage();
        int i = Integer.parseInt(currentPage);
        String pageSize = pageVo.getPageSize();
        int i1 = Integer.parseInt(pageSize);

        LambdaQueryWrapper<User> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        if(user.getId()==null){
            userLambdaQueryWrapper.like(User::getUsername ,user.getUsername())
                    .like(User::getIsvip,user.getIsvip());
        }else{

            userLambdaQueryWrapper.like(User::getUsername ,user.getUsername());
            userLambdaQueryWrapper.like(User::getId,user.getId()+"");
            userLambdaQueryWrapper.like(User::getIsvip,user.getIsvip());
        }

        Page<User> userPage = new Page(i,i1);
        IPage<User> userIPage = userMapper.selectPage(userPage , userLambdaQueryWrapper);
        System.out.println("总页数： "+userIPage.getPages());
        System.out.println("总记录数： "+userIPage.getTotal());
        List<User> records = userIPage.getRecords();
        resultVo.setList(records);

        Integer integer = userMapper.selectCount(userLambdaQueryWrapper);
        resultVo.setTotal(integer);
        return resultVo;
    }

    @Override
    public ResponseResult adduser(User user) {
        ResponseResult result = new ResponseResult();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        try {
            User user1 = userMapper.selectOne(queryWrapper);
            if (user1!=null){

                result.setCode(0);
                result.setErrorMessage("用户名已被启用，请更换用户名");
            }else {
                int insert = userMapper.insert(user);
                if (insert>0){
                    result.setCode(200);
                    result.setErrorMessage("保存用户数据成功");
                }else{
                    result.setCode(0);
                    result.setErrorMessage("保存失败");
                }
            }
        }catch (Exception e){
            result.setCode(0);
            result.setErrorMessage("用户名存在多个，请查看");
        }
        return result;
    }

    @Override
    public ResponseResult deleteAll(List<String> list, HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        ResponseResult result = new ResponseResult();
        for (String s : list) {
            try{

                int i = userMapper.deleteById(s);
                if (i > 0) {
                    result.setCode(200);
                    result.setErrorMessage("删除成功");
                }else{
                    result.setCode(0);
                    result.setErrorMessage("删除失败");
                }
            }catch (Exception e){
                log.info("用户不存在");
            }
        }
        return result;
    }
@Autowired
    LevelMapper levelMapper;
    @Override
    public ResponseResult register(RegisterVo registerVo) {
        ResponseResult result = new ResponseResult();
        User user=userMapper.findByUsername(registerVo.getUsername());
        if (user!=null){
            result.setCode(300);
            result.setErrorMessage("用户名已存在，请更换");
            return result;
        }
        User user1 = new User();
        user1.setEmail(registerVo.getMail());
        user1.setUsername(registerVo.getUsername());
        user1.setIsvip("否");
        user1.setPassword(registerVo.getPassword());
        int insert = userMapper.insert(user1);
        if (insert>0){
            Level level = new Level();
            level.setName(user1.getUsername());
            level.setIdentify("普通用户");
            level.setUserId(user1.getId()+"");
            level.setPicture("https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png");
            level.setStar(0+"");
            int insert1 = levelMapper.insert(level);
            if (insert1>0){

            result.setCode(200);
            result.setErrorMessage("注册成功");
            }
        }
        return result;
    }

    @Override
    public ResponseResult loginMail(String mail) {
        ResponseResult result = new ResponseResult();
        User user = userMapper.findByMail(mail);
        if (user==null){
            result.setCode(0);
            result.setErrorMessage("用户不存在，请重新登陆");
            return result;
        }
        String token = AppJwtUtil.getToken(Long.valueOf(user.getId()));
        result.setHost(token);
        result.setCode(200);
        return result;
    }

    @Override
    public ResponseResult searchchart(String username) {
        ResponseResult result = new ResponseResult();
        List<UserVo> userVos=userMapper.searchchart(username);
        log.info(userVos.toString());
        result.setCode(200);
        result.setData(userVos);
        return result;
    }
}


